/*
     Copyright 2007 Joseph M. Ferner

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using OpenServers.Net.FtpServer.State;

namespace OpenServers.Net.FtpServer.Command {
    /// <summary>
    ///     <para>
    ///         <b>LOGOUT (QUIT)</b><br/>
    ///         This command terminates a USER and if file transfer is not
    ///         in progress, the server closes the control connection.  If
    ///         file transfer is in progress, the connection will remain
    ///         open for result response and the server will then close it.
    ///         If the user-process is transferring files for several USERs
    ///         but does not wish to close and then reopen connections for
    ///         each, then the REIN command should be used instead of QUIT.
    ///     
    ///         An unexpected close on the control connection will cause the
    ///         server to take the effective action of an abort (ABOR) and a
    ///         logout (QUIT).
    ///     </para>
    ///     <para>
    ///         <b>Format</b><br/>
    ///         <code>QUIT &lt;CRLF&gt;</code>
    ///     </para>
    ///     <para>
    ///         <b>Success:</b>
    ///         <list type="bullet">
    ///             <item><term>221</term><description>Service closing control connection. Logged out if appropriate.</description></item>
    ///         </list>
    ///     </para>
    ///     <para>
    ///         <b>Errors:</b>
    ///         <list type="bullet">
    ///             <item><term>500</term><description>Syntax error, command unrecognized.</description></item>
    ///         </list>
    ///     </para>
    /// </summary>
    public class QuitCommand : BaseCommand {

        /// <summary>
        /// overrides the base functionality because a user doesn't need to be logged in.
        /// </summary>
        public override void Invoke(IFtpServer server, FtpState state, string arguments) {
            PerformCommand(server, state, arguments);
        }

        /// <summary>
        /// Marks the state to be closed.
        /// </summary>
        public override void PerformCommand(IFtpServer server, FtpState state, string arguments) {
            state.Listener.Send(state, server.Messages.Get221());
            state.CloseConnection = true;
        }
    }
}
